/* 表格导出导入的回调方法 */
import {MyExcel} from './saveToExcel.js'
import { var_i18n_getText } from '../i18nUtil/i18nUtil'

// region 表格默认导出方法
export const var_print_defaultTable = async (tableDataList, tableTitle, fullPath,
                                             dataBeforeFn, excelStyleFn = undefined, excelCallBack = undefined) => {


  // region 获取tab名称
  const thisTab = window.$piniaTheme.tabsList.find(tab => tab.fullPath == fullPath)
  let title = Date.now().toString()
  if (thisTab) {
    title = `${thisTab.title}${title}`
  }
  // endregion


  // region 处理表头字段冻结排序
  const newTitleSort = window.$newObj(tableTitle)
  newTitleSort.sort((v1, v2) => {
    const oneIndex = tableTitle.findIndex(row => row.tableV3Key == v1.tableV3Key)
    const twoIndex = tableTitle.findIndex(row => row.tableV3Key == v2.tableV3Key)
    if (v2.fixed == 'left' && v1.fixed == 'left') {
      return oneIndex < twoIndex ? -1 : 1
    }
    if (v1.fixed == 'left') {
      return -1
    } else if (v2.fixed == 'left') {
      return 1
    }
    return oneIndex < twoIndex ? -1 : 1
  })
  tableTitle = newTitleSort

  // 冻结的列数
  let freeColumnName = ''

  // endregion

  // region 处理数据

  // 表格头部字段
  const tableTitleList = []

  // 获取数据结构对象
  const data = []

  for (let i = 0; i < tableDataList.length; i++) {
    const item = tableDataList[i]
    const tempData = []
    let nowColumn = 0
    for (let t = 0; t < tableTitle.length; t++) {
      const titleItem = tableTitle[t]

      const prop = titleItem.prop
      const slot = titleItem.slot
      const html = titleItem.html
      const callBack = titleItem.callBack

      // 跳过更多按钮
      if (slot == 'more' || titleItem.show == false) {
        continue
      }

      let tempColumn = {
        // 表格列数
        column: nowColumn,
        // 表格值/富文本值。
        value: undefined,
        // bool,是否加入excel
        push: true,
        // 表头字段名。如果是返回undefined，这往下获取label值。如果title想要富文本就处理excelCallBack函数
        title: undefined
      }

      // 判断是否有自定义函数
      if (typeof dataBeforeFn == 'function') {
        // 返回对象
        const result = dataBeforeFn(item, titleItem, nowColumn)
        if (result) {
          tempColumn = Object.assign(tempColumn, result)
        }
      }

      // region 需要添加值的
      if (tempColumn.push) {

        // region 获取默认值
        // 如果自定义函数没有进行返回则判断下面逻辑。如果有返回包括空文本都好，就不再获取下面值
        if (tempColumn.value == undefined) {
          if (typeof callBack == 'function') {
            tempColumn.value = callBack(item)
          } else if (typeof html == 'function') {
            tempColumn.value = html(item)
          } else if (slot) {
            // 复杂插槽只能靠dataBeforeFn回调函数去处理
            // region
            if (slot == 'isStop') {
              tempColumn.value = item.isStop == 1 ? '停用' : '启用'
            }

            // endregion

          } else if (prop) {
            if (titleItem.type == 'index') {
              tempColumn.value = `${i + 1}`
            } else if (titleItem.type == 'selection') {
              tempColumn.value = ''
              tempColumn.push = false
            } else {
              tempColumn.value = String(item[prop] == null || item[prop] == undefined ? '' : item[prop])
            }
          }
        }
        // endregion

        if (tempColumn.push) {

          // region 获取表头字段
          // 如果自定义函数没有返回表头字段值则判断下面逻辑
          if (tempColumn.title == undefined || tempColumn.title == null) {
            if (titleItem.i18Text) {
              tempColumn.title = var_i18n_getText(titleItem.i18Text)
            } else {
              tempColumn.title = titleItem.label
            }
          }
          const haveTitle = tableTitleList.find(row => row == tempColumn.title)
          if (!haveTitle) {
            tableTitleList.push(tempColumn.title)
          }
          // endregion

          // 标记冻结列的名称
          if (titleItem.fixed == 'left') {
            freeColumnName = tempColumn.title
          }
          nowColumn += 1
          tempData.push(tempColumn)
        }
      }
      // endregion

    }

    data.push(tempData)
  }


  // endregion


  // region 创建excel对象
  const excel = new MyExcel()


  const defaultStyle = {
    fill: {},
    font: {
      size: 11
    },
    alignment: {
      vertical: 'middle',
      horizontal: 'center',
      wrapText: true,
    },
    border: {},
  }

  excel.addWorksheet(title)
  // endregion


  // region 添加头部字段
  // console.log(234,tableTitleList)
  tableTitleList.forEach((item, index) => {
    const thisCell = excel.getCell(1, index + 1)

    thisCell.value = item != undefined ? item.trim() : ''
    // 样式
    if (typeof excelStyleFn == 'function') {
      const thisStyle = excelStyleFn('label', item, index)
      Object.assign(thisCell, thisStyle)
    } else {
      Object.assign(thisCell, window.$newObj(defaultStyle))
    }
  })

  const freeColumn = tableTitleList.findIndex(row => row == freeColumnName)
  excel.freeze(1, freeColumn + 1)

  // endregion


  // region 添加值

  data.forEach((item, index) => {
    item.forEach((item2, index2) => {
      const thisCell = excel.getCell(index + 2, index2 + 1)
      if (typeof item2.value == 'string') {
        thisCell.value = item2.value.trim()
      } else {
        if (item2.value != undefined && item2.value != null) {
          thisCell.value = item2.value
        } else {
          thisCell.value = ''
        }
      }
      if (typeof excelStyleFn == 'function') {
        const thisStyle = excelStyleFn('value', item2.value, index2)
        Object.assign(thisCell, thisStyle)
      } else {
        Object.assign(thisCell, window.$newObj(defaultStyle))
      }
    })
  })

  // endregion

  if (typeof excelCallBack == 'function') {
    await excelCallBack(excel)
  }
  excel.save(title)
}


// endregion